#include "../mqserver/consumer.hpp"

#include <gtest/gtest.h>

mymq::ConsumerManager::ptr cmp;

class ConsumerTest: public testing::Environment
{
    virtual void SetUp() override
    {
        cmp = std::make_shared<mymq::ConsumerManager>();
        cmp->InitQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        cmp->Clear();
        std::cout << "完成资源的清理化工作" << std::endl;
    }
};

void cb(const std::string& tag, const mymq::BasicProperties *bp, const std::string& body)
{
    std::cout << tag << "消费了消息" << body << std::endl;
}

TEST(consumer_test, insert_test)
{
    cmp->Create("consumer1", "queue1", false, cb);
    cmp->Create("consumer2", "queue1", false, cb);
    cmp->Create("consumer3", "queue1", false, cb);

    ASSERT_EQ(cmp->Exists("consumer1", "queue1"), true);
    ASSERT_EQ(cmp->Exists("consumer2", "queue1"), true);
    ASSERT_EQ(cmp->Exists("consumer3", "queue1"), true);
}

TEST(consumer_test, remove_test)
{
    cmp->Remove("consumer1", "queue1");
    ASSERT_EQ(cmp->Exists("consumer1", "queue1"), false);
    ASSERT_EQ(cmp->Exists("consumer2", "queue1"), true);
    ASSERT_EQ(cmp->Exists("consumer3", "queue1"), true);
}

TEST(consumer_test, choose_test)
{
    mymq::Consumer::ptr cp1 = cmp->Choose("queue1");
    ASSERT_NE(cp1.get(), nullptr);
    ASSERT_EQ(cp1->tag, "consumer2");

    mymq::Consumer::ptr cp2 = cmp->Choose("queue1");
    ASSERT_NE(cp2.get(), nullptr);
    ASSERT_EQ(cp2->tag, "consumer3");

    mymq::Consumer::ptr cp3 = cmp->Choose("queue1");
    ASSERT_NE(cp3.get(), nullptr);
    ASSERT_EQ(cp3->tag, "consumer2");
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    RUN_ALL_TESTS();

    return 0;
}